package com.itcam.list_.collection_.collectionsutils;

/**
 * Author:Cammy.Wu
 * Date:2024-06-05
 * Description:
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * static<T> boolean addAll(Collections<? super T> c , T ... elements)     给集合对象批量添加元素
 * static void shuffle(List<?> list)                                       打乱List集合元素的顺序
 * static<T> int binarySearch(List<?> list , T key)                        以二分查找法查找元素
 * static<T> void max/min(Collections<?> c)                                根据默认的自然排序获取最大/最小值
 * static<T> void swap(List<?> list , int i , int j)                       交换集合中指定位置的元素
 */
public class CollectionsDemo {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        Collections.addAll(arrayList, "a", "b", "c", "d", "e", "f");
        System.out.println("arrayList = " + arrayList);

//        Collections.shuffle(arrayList);
//        System.out.println("arrayList = " + arrayList);

        int index = Collections.binarySearch(arrayList, "f");
        System.out.println("index = " + index);

        System.out.println(Collections.max(arrayList));
        System.out.println(Collections.min(arrayList));

        Collections.swap(arrayList, 0, 5);
        System.out.println("arrayList = " + arrayList);

        // sort:对集合进行排序，默认升序

        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list, 1, 2, 3, 5, 4, 6, 7);
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
//                return o1 - o2;   //升序
                return o2 - o1; //降序
            }
        });
        System.out.println("list = " + list);


    }
}
